package com.kfm.boot.controller;

import com.kfm.boot.commons.Constant;
import com.kfm.boot.entity.Chat;
import com.kfm.boot.http.HttpExample;
import com.kfm.boot.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;

/**
   @RestController = @Controller + @ResponseBody
 */
@RestController
@CrossOrigin
public class HelloController {

    @Autowired
    private ChatService chatService;

    /**
     * 处理 /hello 请求的方法
     * 在spring中把这样的方法称为 handler
     * @return
     */
    @RequestMapping("/hello")
    public ModelAndView hello(){
        // 逻辑视图。 不做任何配置对应的物理视图是 `resources目录下`的 `templates/hello.html`
        return new ModelAndView("hello");
    }

    @RequestMapping("/form")
    public ModelAndView form(){
        return new ModelAndView("form");
    }

    @RequestMapping("/submit")
    public String submit(String user, String password){
        return "success: " + user + " , " + password;
    }

    @GetMapping("/chat")
    public ModelAndView chat(){
        return new ModelAndView("chat");
    }

    @GetMapping("/ajax")
    public ModelAndView ajax(){
        return new ModelAndView("ajax");
    }

    @RequestMapping("/q")
    public String q(@RequestBody String message){
        return "Answer: 当前时间是 " + new Date() + " , 消息是 " + message;
    }

    @PostMapping("/startChat")
    public String startChat(@RequestBody String msg) {
        String result = HttpExample.send(Constant.URL, msg);
        System.out.println(result);
        Chat chat = new Chat();
        chat.setModel(Constant.GPT_35_TURBO);
        chat.setQuestion(msg);
        chat.setAnswer(result);
        chatService.save(chat);
        return result;
    }

    @RequestMapping("/test")
    public String test(){
        Chat chat = new Chat();
        chat.setModel("gpt-3.5-trubo");
        chat.setQuestion("你好");
        chat.setAnswer("你好");
        chatService.save(chat);
        return "success";
    }
}
